package com.tanhua.dubbo.api.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.SoultestResult;
import com.tanhua.dubbo.mapper.SoultestResultMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class SoultestResultApiImpl implements SoultestResultApi {
    @Autowired
    private SoultestResultMapper soultestResultMapper;

    @Override
    public SoultestResult getResult(Long userId, Long paperId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("paper_id",paperId);
        SoultestResult soultestResult =soultestResultMapper.selectOne(queryWrapper);
        return soultestResult;
    }

    @Override
    public SoultestResult getBigPaperId(Long userId) {
        QueryWrapper<SoultestResult> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByDesc("paper_id");
        queryWrapper.last("limit 1");
        return soultestResultMapper.selectOne(queryWrapper);
    }

    @Override
    public SoultestResult getById(Long id) {
        return soultestResultMapper.selectById(id);
    }

    @Override
    public List<SoultestResult> getSimilarYou(Long type) {
        //这里使用了自定义语句的语法
        return soultestResultMapper.getSimilarYou(type);
    }

    @Override
    public String saveReport(SoultestResult soultestResult) {
        int result = soultestResultMapper.insert(soultestResult);
        //非空判断
        if(!ObjectUtils.isEmpty(result)){
            return String.valueOf(result);
        }
        return null;
    }

    @Override
    public SoultestResult findOne(SoultestResult soultestResult) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",soultestResult.getUserId());
        queryWrapper.eq("paper_id",soultestResult.getPaperId());
        return soultestResultMapper.selectOne(queryWrapper);
    }

    @Override
    public void update(SoultestResult soultestResult) {
        soultestResultMapper.updateById(soultestResult);
    }
}
